package gang.com.twoshopping.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import gang.com.twoshopping.R;

/**
 * Created by Administrator on 2017/5/14.
 */
//这里是一个泛型，需要一个viewholder，新建的viewholder也需要继承
public class MyAdapterTongji extends RecyclerView.Adapter<MyAdapterTongji.viewholder> {
    private List<String> mDates;
    private LayoutInflater inflater;
    private OnItemClickListener listener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public MyAdapterTongji(List<String> datas) {
        mDates = datas;
    }

    //添加数据的方法
    public void addDate(int position, String city) {
        mDates.add(position, city);
        //更新到那一条通知那条
        notifyItemInserted(position);
    }

    //删除数据
    public void removeData(int position) {
        mDates.remove(position);
        notifyItemRemoved(position);
    }

    //创建一个viewHolder的方法，相当于getview的方法
    @Override
    public viewholder onCreateViewHolder(ViewGroup parent, int viewType) {
        inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.item, parent, false);
        return new viewholder(view);
    }

    @Override
    public void onBindViewHolder(viewholder holder, int position) {
        holder.textView.setText(mDates.get(position));
    }

    @Override
    public int getItemCount() {
        return mDates.size();
    }

    class viewholder extends RecyclerView.ViewHolder {
        private TextView textView;

        public viewholder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.item_text);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onClick(v, getLayoutPosition(), mDates.get(getLayoutPosition()));
                    }
                }
            });
        }
    }


    public interface OnItemClickListener {
        void onClick(View v, int position, String city);
    }
}
